package uk.org.scuts.bantstadium2;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

public class Utils {
	
	static public InputStream OpenXML(Activity ctx, String primaryFile, String fallbackFile)
	{
		final String methodName = "OpenXML";
		InputStream ret = null;
		try {
			ret = ctx.openFileInput(primaryFile);
			Log.i(methodName, "Opened " + primaryFile);
		} catch (FileNotFoundException e) {
			try {
				Log.i(methodName, "Failed to open " + primaryFile);
				ret = ctx.getResources().getAssets().open(fallbackFile);
				Log.i(methodName, "Opened " + fallbackFile);
			} catch (IOException e1) {
				e1.printStackTrace();
				Log.i(methodName, "Failed to open " + fallbackFile);
				System.exit(1);
			}
		}
		return ret;
	}
	
	static public Document ParseXML(InputStream iStream) {
		Document ret = null;
	    try {
	    	InputSource source = new InputSource(iStream);
	        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	        dbf.setNamespaceAware(false);
	        dbf.setValidating(false);
	        DocumentBuilder db = dbf.newDocumentBuilder();
	        ret = db.parse(source);
	        ret.getDocumentElement().normalize();
	    } catch (Exception e) {
	        e.printStackTrace();
	        System.exit(1);
	    }
	    return ret;
	 }
	
	static public void SaveXML(Activity ctx, Document doc, String fileName)
	{
		final String methodName = "SaveXML";
		try {
			FileOutputStream fos = ctx.openFileOutput(fileName, Context.MODE_PRIVATE);

		    TransformerFactory tFactory =
		    TransformerFactory.newInstance();
		    Transformer transformer = tFactory.newTransformer();

		    DOMSource source = new DOMSource(doc);
		    StreamResult result = new StreamResult(fos);
		    transformer.transform(source, result);
			fos.close();
			
			Log.i(methodName, fileName + " saved");
		} catch (Exception e) {
			Log.i(methodName, "Failed to write to " + fileName);
			e.printStackTrace();
			System.exit(1);
		}
	}
	
	static public void DeleteXML(Activity ctx, String fileName)
	{
		final String methodName = "DeleteXML";
		if (ctx.deleteFile(fileName))
		{
			Log.i(methodName, fileName + " deleted");
		}
	}
	
	static public ArrayList<String> GetNameList(Document doc) {
		
		ArrayList<String> nameList = new ArrayList<String>();
		
		NodeList nList = doc.getElementsByTagName("character");
	 
		for (int i = 0; i < nList.getLength(); i++) {
	 
			Node nNode = nList.item(i);
	 
			if (nNode.getNodeType() == Node.ELEMENT_NODE) {
				nameList.add(((Element) nNode).getElementsByTagName("name").item(0).getTextContent());
			}
		}
		
		Collections.sort(nameList);
		
		return nameList;
	}

	static public String CharsFilename()
	{
		return "characters.xml";
	}
	
	static public String CustomCharsFilename()
	{
		return "custom_chars.xml";
	}
}
